var packageName = "com.xingin.xhs"; // 视频App的包名

// 打开视频App
launch(packageName);
toast('打开小红书')

// App加载完成
waitForPackage(packageName);
toast('加载完成')
sleep(5000)

const searchXHS = className('android.widget.Button').desc('搜索').findOne(2000)
if(!searchXHS){
    console.log('没有搜索按钮')
    back()
}
searchXHS.click()
sleep(random(1000,3000))
const arr = ['9774287415','556451136']
arr.forEach(e=>{
    getSearchXHS(e)
})




function getSearchXHS(e){
    const keywordID = 1
    const searchInputXHS = className('android.widget.EditText').depth('9').drawingOrder('2').findOne(2000)
    if(!searchInputXHS){
        toastLog('没有搜索框')
    }
    if(keywordID){
        searchInputXHS.setText('餐饮')
        const searchBtnXHS = className('android.widget.Button').depth('8').drawingOrder('3').text('搜索').findOne(2000)
        if(!searchBtnXHS){
            toastLog('没有搜索按钮')
        }
        searchBtnXHS.click()
        sleep(random(1000,3000))
    }else{
        // 关注账号
        searchInputXHS.setText(e)
        const searchBtnXHS = className('android.widget.Button').depth('8').drawingOrder('3').text('搜索').findOne(2000)
        if(!searchBtnXHS){
            toastLog('没有搜索按钮')
        }
        searchBtnXHS.click()
        sleep(random(1000,3000))

        const userBtnXHS = className('android.widget.TextView').depth('14').drawingOrder('2').text('用户').findOne(2000)
        if(!userBtnXHS){
            toastLog('没有用户按钮')
        }
        click(userBtnXHS.bounds().centerX(),userBtnXHS.bounds().centerY())
        sleep(random(1000,3000))
        const userXHS = className('android.widget.RelativeLayout').depth('10').drawingOrder('2').findOne(2000).click()
        const noneXHS = className('android.widget.TextView').depth('19').drawingOrder('4').text('TA还没有发布笔记哦~').findOne(2000)
        if(noneXHS){
            toastLog('没有笔记')
            back()
            sleep(500)
            back()
            sleep(random(1000,3000))
        }else{
            jieLiuXHS()
            back()
            sleep(500)
            back()
            sleep(random(1000,3000))
        }
    }
    
}






function jieLiuXHS(){
    const worksXHS = className('android.widget.FrameLayout').depth('18').find()
    log('work长度' + worksXHS.length)
    worksXHS.forEach(e=>{
        e.click()
        sleep(random(1000,3000))
        const reviewXHS = className('android.widget.Button').depth('10').drawingOrder('4').findOne(2000)
        if(reviewXHS){
            if(reviewXHS.child(1).text()=='评论'){
                toastLog('没有评论')
                back()
                sleep(random(1000,3000))
            }
            reviewXHS.click()
            replyXHS()
        }else{
            const reviewXHSBtn = className('android.widget.Button').depth('10').drawingOrder('32').findOne(2000)
            if(reviewXHSBtn.child(1).text()=='评论'){
                toastLog('没有评论')
                back()
                sleep(random(1000,3000))
            }else{
                reviewXHSBtn.click()
                sleep(random(1000,3000))
                replyXHS2()
            }
        }
    })
}


// 生成随机数函数
function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }


// 截流回复函数
function replyXHS(){
    let reviewXHSList = className('android.widget.FrameLayout').depth('10').find()
    reviewXHSList = reviewXHSList.filter(e=>e.child(0)&&e.child(0).childCount()==1)
    reviewXHSList = reviewXHSList.filter(e=>e.child(0)&&e.child(0).child(0).childCount()==3)
    reviewXHSList.forEach(e=>{
        console.log(e.child(0).child(0).child(1).child(1).text().split(' ')[0]);
        jieLiuReviewXHS(e)
    })
    const endXHS = className('android.widget.TextView').depth('12').drawingOrder('2').text('- 到底了 -').findOne(2000)
    if(endXHS){
        toastLog('到底了')
        back()
        sleep(random(1000,3000))
    }else{
        swipe(
            device.width / 2,
            device.height * 0.8,
            device.width / 2,
            device.height * 0.2,
            random(500,1000)
        )
        random(500,1000)
        replyXHS()
    }
}

function replyXHS2(){
    let reviewXHSList = className('android.widget.LinearLayout').depth('14').find()
    console.log(reviewXHSList.length);
    reviewXHSList.forEach(e=>{
        console.log(e.child(0).child(1).child(1).text().split(' ')[0]);
        jieLiuReviewXHS(e)
    })
    const endXHS = className('android.widget.TextView').depth('15').drawingOrder('2').text('- 到底了 -').findOne(2000)
    if(endXHS){
        toastLog('到底了')
        back()
        sleep(500)
        back()
        sleep(random(1000,3000))
    }else{
        swipe(
            device.width / 2,
            device.height * 0.8,
            device.width / 2,
            device.height * 0.2,
            800
        )
        sleep(random(1000,1500))
        replyXHS2()
    }
}


function jieLiuReviewXHS(e){
    let childTextXHS = e.child(0).childCount()==1?e.child(0).child(0).child(1).child(1).text().split(' ')[0]:e.child(0).child(1).child(1).text().split(' ')[0]
        e.child(0).childCount()==1?e.child(0).child(0).child(0).click():e.child(0).child(0).click()
        const XHSHao = className("android.widget.TextView").depth("17").drawingOrder("2").findOne(6000)
        const XHSIP = className("android.widget.TextView").depth("15").drawingOrder("16").findOne(6000)
        console.log(XHSHao.text(),XHSIP.text());
        back()
        sleep(3000)
        // 新消息
        // let input = className("android.widget.EditText").depth("10").drawingOrder("1").findOne(6000)
        // if(input){
        //     input.setText('1')
        //     sleep(random(1000,3000))
        //     className("android.widget.TextView").text("发送").depth("10").drawingOrder("3").findOne().click()
        //     console.log('发送');
        //     sleep(random(1000,3000))
        //     back()
        //     sleep(random(1000,3000))
        //     back()
        //     sleep(random(1000,3000))
        // }
}